/**
 * 
 */
package cn.pb.utils;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * @ClassName SendEmailUtil类
 * @Author whw
 * @Date 2017年9月7日 下午4:50:22
 * @Version 1.0
 * @Description 
 */
public class SendEmailUtil {
    private static Properties prop;
    private static Session session;
    static{
        prop = new Properties();
        prop.setProperty("mail.host", "smtp.qq.com");
        prop.put("mail.smtp.port","587");
        prop.setProperty("mail.transport.protocol", "smtp");
        prop.setProperty("mail.smtp.auth", "true");
        session = Session.getInstance(prop);
    }
    public SendEmailUtil() {
    }
    public static boolean sendEmail(String tittle,String sendmessage,String email) {
        Transport ts;
        try {
            ts = session.getTransport();
             //3、使用邮箱的用户名和密码连上邮件服务器，发送邮件时，发件人需要提交邮箱的用户名和密码给smtp服务器，用户名和密码都通过验证之后才能够正常发送邮件给收件人。
             ts.connect("smtp.qq.com", "954433942@qq.com", "vonpwurldnngbbdd");
             //4、创建邮件
             Message mm = new MimeMessage(session);
             //指明邮件的发件人
             mm.setFrom(new InternetAddress("954433942@qq.com"));
             //指明邮件的收件人
             mm.setRecipient(Message.RecipientType.TO, new InternetAddress(email));
             //邮件的标题
             mm.setSubject(tittle);
             //邮件的文本内容
             mm.setContent(sendmessage, "text/html;charset=UTF-8");
             //5、发送邮件
             ts.sendMessage(mm, mm.getAllRecipients());
             ts.close();
             return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
